package com.woniu.humanresource.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.humanresource.dao.AttendanceParametersMapper;
import com.woniu.humanresource.dto.PageDto;
import com.woniu.humanresource.entity.*;
import com.woniu.humanresource.exception.GeneralException;
import com.woniu.humanresource.service.AttendanceParametersService;
import com.woniu.humanresource.service.AttendanceStyleService;
import com.woniu.humanresource.utils.ReflectionUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyq
 * @since 2022-07-11
 */
@RestController
@RequestMapping("/humanresource/attendance-style")
public class AttendanceStyleController {

    @Autowired
    private AttendanceStyleService attendanceStyleService;

    //分页类别信息
    @PostMapping("/attendanceStyle")
    public ResponseText<IPage<AttendanceStyle>> getAttendanceStyle(@RequestBody PageDto<AttendanceStyle,AttendanceStyle> pageDto) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        AttendanceStyle searchExample = pageDto.getSearchExample();
        Page<AttendanceStyle> page = pageDto.getPage();
        IPage<AttendanceStyle> resultPage;
        if (ObjectUtils.anyNotNull(searchExample)){
            QueryWrapper<AttendanceStyle> queryWrapper = ReflectionUtils.createPageWrapper2Field(searchExample, new QueryWrapper<>());
            resultPage = this.attendanceStyleService.page(page, queryWrapper);
        }else {
            resultPage = this.attendanceStyleService.page(page,null);
        }
        return new ResponseText<IPage<AttendanceStyle>>(resultPage,200,"success");
    }
    //更新
    @PutMapping("/update")
    public ResponseText update(@RequestBody AttendanceStyle attendanceStyle){
        boolean b = attendanceStyleService.updateById(attendanceStyle);
        return new ResponseText(b,200,"success");
    }
    //删除
    @DeleteMapping("/deleteById/{id}")
    public ResponseText delete(@PathVariable("id")int id){
        boolean b = attendanceStyleService.removeById(id);
        return new ResponseText(b,200,"success");
    }
    //导入员工信息
    @PostMapping("/importAttendanceStyle")
    public ResponseText importEmployees(MultipartFile attendanceStyleExcel){
        ResponseText responseText=null;
        try {
            attendanceStyleService.importAttendanceStyle(attendanceStyleExcel);
            responseText=new ResponseText(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    //    导出员工信息
    @GetMapping("/exportAttendanceStyle")
    public ResponseText exportEmployeeExcel(HttpServletResponse response) {
        ResponseText responseText=null;
        try {
            attendanceStyleService.exportAttendanceStyle(response);
            responseText=new ResponseText(null,200,"success");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }

}

